package com.suneast.suneaststore.modules.personal.sign;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.personal.sign.event.ModifyPwdResult;
import com.suneast.suneaststore.modules.personal.sign.event.RequestAutoCodeResult;
import com.suneast.suneaststore.modules.personal.sign.event.ResetPwdResult;
import com.suneast.suneaststore.modules.personal.sign.event.SignInResult;
import com.suneast.suneaststore.modules.personal.sign.event.SignUpResult;
import com.suneast.suneaststore.util.log.LatteLogger;
import com.suneast.suneaststore.util.net.RestClient;

import org.greenrobot.eventbus.EventBus;


public class SignRequestHandler {

    public static void requestAuthCode(String phoneNumber, @Flag.CodeType int type) {
        Log.e("test", "json requestAuthCode: number = " + phoneNumber);
        RestClient.builder()
                .url(API.SEND_SMS)
                .params("mobile", phoneNumber)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject rootJObj = JSONObject.parseObject(response);
                    if (rootJObj.getIntValue("code") == 1) {
                        EventBus.getDefault().post(
                                new RequestAutoCodeResult(Flag.Result.SUCCEED, type));
                    } else {
                        EventBus.getDefault().post(
                                new RequestAutoCodeResult(Flag.Result.FAILURE, type));
                    }
                })
                .failure(() -> {
                    LatteLogger.e("error", "requestAuthCode failure");
                    EventBus.getDefault().post(
                            new RequestAutoCodeResult(Flag.Result.FAILURE, type));
                })
                .error((int code, String msg) -> {
                    LatteLogger.e("error", "requestAuthCode error code = " + code + "msg = " + msg);
                    EventBus.getDefault().post(
                            new RequestAutoCodeResult(Flag.Result.FAILURE, type));
                })
                .build()
                .post();
    }


    static void signUp(String phoneNumber, String autoCode, String pwd) {
        Log.e("test", "json signUp: ");
        RestClient.builder()
                .url(API.SIGN_UP)
                .params("from", "android")
                .params("mobile", phoneNumber)
                .params("phoneverify", autoCode)
                .params("password", pwd)
                .success(response -> {
                    LatteLogger.json("json", response);
                    JSONObject rootObj = JSON.parseObject(response);
                    if (rootObj.getIntValue("code") == 1) {
                        JSONObject dataObj = rootObj.getJSONObject("data");
                        AccountManager.update(true,
                                dataObj.getIntValue("level"),
                                dataObj.getString("token"),
                                dataObj.getString("sign"));
                    } else {
                        SignUpResult result = new SignUpResult(Flag.Result.FAILURE);
                        result.setMsg(rootObj.getString("msg"));
                        EventBus.getDefault().post(result);
                        LatteLogger.e("error", "signUp success but code =" +
                                rootObj.getIntValue("code") + " msg = "
                                + rootObj.getString("msg")
                        );
                    }
                })
                .failure(() -> {
                    LatteLogger.e("error", "signUp failure");
                    EventBus.getDefault().post(new SignUpResult(Flag.Result.FAILURE));
                })
                .error((int code, String msg) -> {
                    LatteLogger.e("error", "signUp error code = "
                            + code + " msg = " + msg);
                    EventBus.getDefault().post(new SignUpResult(Flag.Result.FAILURE));
                })
                .build()
                .post();
    }

    static void signInByPassWord(String phoneNumber, String pwd) {
        RestClient.builder()
                .url(API.SIGN_IN)
                .params("username", phoneNumber)
                .params("password", pwd)
                .params("method", "main")
                .success(response -> {
                    JSONObject rootJObj = JSONObject.parseObject(response);
                    LatteLogger.json("test", response);
                    if (rootJObj.getIntValue("code") == 1) {
                        JSONObject dataObj = rootJObj.getJSONObject("data");
                        AccountManager.update(true,
                                dataObj.getIntValue("level"),
                                dataObj.getString("token"),
                                dataObj.getString("sign"));
                        EventBus.getDefault().post(new SignInResult(
                                Flag.Result.SUCCEED, Flag.CodeType.SIGN_UP));
                    } else {
                        EventBus.getDefault().post(new SignInResult(
                                Flag.Result.FAILURE, Flag.CodeType.SIGN_UP));
                    }
                })
                .failure(() -> {
                    LatteLogger.e("error", "signInByPassWord failure");
                    EventBus.getDefault().post(new SignInResult(
                            Flag.Result.FAILURE, Flag.CodeType.SIGN_UP));
                })
                .error((int code, String msg) -> {
                    LatteLogger.e("error", "signInByPassWord error code = " + code + "msg = " + msg);
                    EventBus.getDefault().post(new SignInResult(
                            Flag.Result.FAILURE, Flag.CodeType.SIGN_UP));
                })
                .build()
                .post();
    }

    static void signInByAuthCode(String phoneNumber, String authCode) {
        RestClient.builder()
                .url(API.SIGN_IN)
                .params("username", phoneNumber)
                .params("method", "sms")
                .params("phoneverify", authCode)
                .success(response -> {
                    JSONObject rootJObj = JSONObject.parseObject(response);
                    LatteLogger.json("test", response);
                    if (rootJObj.getIntValue("code") == 1) {
                        JSONObject dataObj = rootJObj.getJSONObject("data");
                        AccountManager.update(true,
                                dataObj.getIntValue("level"),
                                dataObj.getString("token"),
                                dataObj.getString("sign"));
                        EventBus.getDefault().post(new SignInResult(
                                Flag.Result.SUCCEED, Flag.CodeType.AUTH_CODE));
                    } else {
                        EventBus.getDefault().post(new SignInResult(
                                Flag.Result.FAILURE, Flag.CodeType.AUTH_CODE));
                    }
                })
                .failure(() -> {
                    EventBus.getDefault().post(new SignInResult(
                            Flag.Result.FAILURE, Flag.CodeType.AUTH_CODE));
                    LatteLogger.e("error", "signInByAuthCode failure");
                })
                .error((int code, String msg) -> {
                    EventBus.getDefault().post(new SignInResult(
                            Flag.Result.FAILURE, Flag.CodeType.AUTH_CODE));
                    LatteLogger.e("error", "signInByAuthCode error code = " + code + "msg = " + msg);
                })
                .build()
                .post();
    }


    public static void resetPwd(String phoneNumber, String authCode, String pwd, String confirmPwd) {
        RestClient.builder()
                .url(API.RESET_PWD)
                .params("mobile", phoneNumber)
                .params("phoneverify", authCode)
                .params("pwd", pwd)
                .params("newpwd", confirmPwd)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject rootJObj = JSON.parseObject(response);
                    if (rootJObj.getIntValue("code") == 1) {
                        EventBus.getDefault().post(new ResetPwdResult(Flag.Result.SUCCEED));
                    } else {
                        EventBus.getDefault().post(new ResetPwdResult(Flag.Result.FAILURE, rootJObj.getString("msg")));
                    }
                })
                .failure(() -> {
                    Log.e("error", "resetPwd failure");
                    EventBus.getDefault().post(new ResetPwdResult(Flag.Result.FAILURE));
                })
                .error((int code, String msg) -> {
                    Log.e("error", "resetPwd error code = " + code + "msg = " + msg);
                    EventBus.getDefault().post(new ResetPwdResult(Flag.Result.FAILURE));
                })
                .build()
                .post();
    }

    public static void modifyPwd(String oldPwd, String newPwd) {
        RestClient.builder()
                .url(API.CHANGE_PWD)
                .params("token", AccountManager.getToken())
                .params("password", oldPwd)
                .params("newpassword", newPwd)
                .params("renewpassword", newPwd)
                .success(response -> {
                    LatteLogger.json("test", response);
                    JSONObject rootJObj = JSON.parseObject(response);
                    if (rootJObj.getIntValue("code") == 1) {
                        EventBus.getDefault().post(new ModifyPwdResult(Flag.Result.SUCCEED));
                    } else {
                        Log.e("error", "modifyPwd failure in response");
                        EventBus.getDefault().post(new ModifyPwdResult(Flag.Result.FAILURE));
                    }
                })
                .failure(() -> {
                    Log.e("error", "modifyPwd failure");
                    EventBus.getDefault().post(new ModifyPwdResult(Flag.Result.FAILURE));
                })
                .error((int code, String msg) -> {
                    Log.e("error", "modifyPwd error code = " + code + "msg = " + msg);
                    EventBus.getDefault().post(new ModifyPwdResult(Flag.Result.FAILURE));
                })
                .build()
                .post();
    }
}
